package com.fenrirtec.aepp.common.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.condition.MemberCondition;
import com.fenrirtec.aepp.common.dao.TenderQuoteDao;
import com.fenrirtec.aepp.common.dao.impl.TenderQuoteDao_JDBCImpl;
import com.fenrirtec.aepp.common.dto.TenderQuoteListDto;
import com.fenrirtec.aepp.common.model.TenderQuoteList;
import com.fenrirtec.aepp.common.service.TenderQuoteService;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.BeanCopierUtils;

public class TenderQuoteServiceImpl implements TenderQuoteService{
	
	private static final Logger logger = LoggerFactory.getLogger(TenderServiceImpl.class);

    @Override
    public List<TenderQuoteListDto> searchByCnd(MemberCondition condition) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderQuoteServiceImpl#searchByCnd] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderQuoteList> tenderQuoteList = null;

        try {
            connection.setAutoCommit(false);
            TenderQuoteDao tenderQuoteDao = new TenderQuoteDao_JDBCImpl();
            tenderQuoteList = tenderQuoteDao.searchByCnd(condition);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderQuoteServiceImpl#searchByCnd] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderQuoteServiceImpl#searchByCnd] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderQuoteServiceImpl#searchByCnd] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderQuoteListDto> tenderQuoteDtoList = null;
        
        if (tenderQuoteList != null && tenderQuoteList.size() > 0) {
        	tenderQuoteDtoList = new ArrayList<TenderQuoteListDto>();
            for (TenderQuoteList tenderQuote : tenderQuoteList) {
                TenderQuoteListDto tenderQuoteDto = new TenderQuoteListDto();
                BeanCopierUtils.copyProperties(tenderQuote, tenderQuoteDto);
                tenderQuoteDtoList.add(tenderQuoteDto);
            }
        }
        
        return tenderQuoteDtoList;
    }
}
